package com.waypolice.manager.controller;

import com.waypolice.common.pojo.AppraisalCommonResult;
import com.waypolice.common.pojo.AppraisalItemResult;
import com.waypolice.common.pojo.DestroyCommonResult;
import com.waypolice.common.pojo.TaotaoResult;
import com.waypolice.common.utils.PageUtils;
import com.waypolice.manager.service.*;
import com.waypolice.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * 档案著录
 * @author zry
 *
 */
@Controller
@Api(value = "档案", description = "档案鉴定")
@RequestMapping(value="/pcwindow")
public class ArcAppraisalAction {

	private static final Logger LOGGER = LoggerFactory.getLogger(ArcAppraisalAction.class);

    @Autowired
    private ArcAppraisalService arcAppraisalService;
    @Autowired
    private VolAndItemService volAndItemService;
    @Autowired
    private UpmsArchivesTypesService upmsArchivesTypesService;
    @Autowired
    private UpmsDictionaryService upmsDictionaryService;
	/**
	 * 档案鉴定页面跳转
	 *
	 * @param map
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/arcAppraisal")
	public String appManagerJump(ModelMap map, HttpServletResponse response,
								  HttpServletRequest request) {
        String id = request.getParameter("id");
        //当前搜索的文件类型：卷或者件
        String type =request.getParameter("type");
        request.setAttribute("type",type);
        request.setAttribute("id",id);
		return "/appraisalArchive/checkupArchive";
	}
    @ApiOperation(value = "根据条件查询卷件")
    @RequestMapping(value = "/searchArchiveMessage", method = RequestMethod.GET)
    @ResponseBody
    public Object searchAllMessage(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            @RequestParam(required = false, value = "id") String id,
            @RequestParam(required = false, value = "type") String type) {
        Map<String, Object> result = new HashMap<>(2);
        if("0".equals(type)){
            //查询卷
            List<CmsJudVolume> volumes = arcAppraisalService.selectVolumeByVolId(Long.valueOf(id));
            List<CmsJudVolume> finalList = new ArrayList<CmsJudVolume>();
            PageUtils<CmsJudVolume> pageUtils = new PageUtils<CmsJudVolume>();
            finalList = pageUtils.pageList(volumes,offset,limit);
            result.put("rows",finalList);
            result.put("total",volumes.size());
        }else if("1".equals(type)){
            //查询件
            List<CmsJudItem> items = arcAppraisalService.selectItemByItemId(Long.valueOf(id));
            List<CmsJudItem> finalList = new ArrayList<CmsJudItem>();
            PageUtils<CmsJudItem> pageUtils = new PageUtils<CmsJudItem>();
            finalList = pageUtils.pageList(items,offset,limit);
            result.put("rows",finalList);
            result.put("total",items.size());
        }else{
            //查询单件
            List<CmsSinItem> itemsin  =  arcAppraisalService.selectSinItemByItemId(Long.valueOf(id));
            List<CmsSinItem> finalList = new ArrayList<CmsSinItem>();
            PageUtils<CmsSinItem> pageUtils = new PageUtils<CmsSinItem>();
            finalList = pageUtils.pageList(itemsin,offset,limit);
            result.put("rows",finalList);
            result.put("total",itemsin.size());
        }
        return result;
    }
    /**
     * 根据itemId查询卷件信息
     * @param response
     * @param request
     * @return
     */
    @ApiOperation(value = "根据itemId查询卷件信息")
    @RequestMapping(value = "/findArchiveMessageByItemId")
    @ResponseBody
    public Object findArchiveMessageByItemId(HttpServletResponse response, HttpServletRequest request) {
        String itemId = request.getParameter("itemId");
        List<Map<String,Object>>  arichives = arcAppraisalService.findArchiveTypeByArchiveNum(itemId);
        return arichives;
    }
    /**
     * 根据itemId查询单件信息
     * @param response
     * @param request
     * @return
     */
    @ApiOperation(value = "根据itemId查询单件信息")
    @RequestMapping(value = "/findArchiveMessageBySinItemId")
    @ResponseBody
    public Object findArchiveMessageBySinItemId(HttpServletResponse response, HttpServletRequest request) {
        String itemId = request.getParameter("itemId");
        List<CmsSinItem> itemsin  =  arcAppraisalService.selectSinItemByItemId(Long.valueOf(itemId));
        return itemsin;
    }

    /**
     * 查询卷内件数量
     * @param response
     * @param request
     * @return
     */
    @ApiOperation(value = "查询卷内件数量")
    @RequestMapping(value = "/findItemNumById")
    @ResponseBody
    public Object findItemNumById(HttpServletResponse response, HttpServletRequest request) {
        String id = request.getParameter("id");
        long itemTotalNum = arcAppraisalService.findItemNumById(id);
        return itemTotalNum;
    }
    /**
     * 添加鉴定信息
     *
     * @return user
     */
    @ApiOperation(value = "添加鉴定信息")
    @RequestMapping(value = "/insertAppraisalMessage")
    @ResponseBody
    public TaotaoResult insertAppraisalMessage(HttpServletRequest request, HttpServletResponse response) {
        TaotaoResult result = new TaotaoResult();
        String apprId = request.getParameter("apprId");
        String apprCode = request.getParameter("apprCode");
        String apprApartment = request.getParameter("apprApartment");
        String apprPerson = request.getParameter("apprPerson");
        String apprDate = request.getParameter("apprDate");
        String volId = request.getParameter("volId");
        String itemId = request.getParameter("itemId");
        String arcType = request.getParameter("arcType");
        String apprOldDate = request.getParameter("apprOldDate");
        String apprRemarks = request.getParameter("apprRemarks");
        String apprArcview = request.getParameter("apprArcview");
        String apprPersonview = request.getParameter("apprPersonview");
        String apprExportsview = request.getParameter("apprExportsview");
        String apprLeaderview = request.getParameter("apprLeaderview");
        String apprScope = request.getParameter("apprScope");
        String apprResult = request.getParameter("apprResult");
        String nextDeadline = request.getParameter("nextDeadline");
        if(StringUtils.isBlank(apprId)){
            //新插入
            result = arcAppraisalService.inserAppraisalMessage(apprCode, apprApartment, apprPerson, apprDate, volId, itemId, arcType,
                    apprArcview,apprPersonview, apprExportsview, apprLeaderview,apprScope,apprResult,nextDeadline,apprOldDate,apprRemarks);
        }else{
            //更新
            result = arcAppraisalService.updateAppraisalMessage(apprId, apprCode, apprApartment, apprPerson, apprDate, volId, itemId, arcType,
                    apprArcview,apprPersonview, apprExportsview, apprLeaderview,apprScope,apprResult,nextDeadline,apprOldDate,apprRemarks);
        }
        return result;
    }

    @ApiOperation(value = "查询所有的鉴定信息")
    @RequestMapping(value = "/searchAppraisalMessage", method = RequestMethod.GET)
    @ResponseBody
    public Object  searchAppraisalMessage(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
            Map<String, Object> result = new HashMap<>(2);
            //查询件
            List<CmsAppraisal> cmsAppraisals =  arcAppraisalService.selectAllAppraisal();
            List<AppraisalCommonResult> apps = new ArrayList<>();
             for (CmsAppraisal cmsAppraisal:cmsAppraisals) {
                 Integer apprType = cmsAppraisal.getApprType();
                 Long apprArcId = cmsAppraisal.getApprArcId();
                 if (apprType == 0) {
                     AppraisalCommonResult appraisalCommonResult = new AppraisalCommonResult();
                     CmsJudVolume cmsJudVolume = volAndItemService.selectCmsJudVolumeByVolId(Long.valueOf(apprArcId));
                     appraisalCommonResult.setApprCode(cmsAppraisal.getApprCode());
                     appraisalCommonResult.setApprDate(cmsAppraisal.getApprDate());
                     appraisalCommonResult.setApprOpinion(cmsAppraisal.getAppExtrafour());
                     appraisalCommonResult.setApprResult(cmsAppraisal.getAppExtratwo());
                     appraisalCommonResult.setApprDealNum(cmsAppraisal.getAppDealcode());
                     appraisalCommonResult.setApprItemName("");
                     appraisalCommonResult.setApprId(cmsAppraisal.getApprId());
                     if(!StringUtils.isBlank(cmsJudVolume.getVolClass())){
                         appraisalCommonResult.setApprVolClass(cmsJudVolume.getVolClass());
                     }
                     if(!StringUtils.isBlank(cmsJudVolume.getVolStorageTime())){
                         appraisalCommonResult.setApprStorageTime(cmsJudVolume.getVolStorageTime());
                     }
                     if(!StringUtils.isBlank(cmsJudVolume.getVolArchiveUnit())){
                         appraisalCommonResult.setApprArcOrg(cmsJudVolume.getVolArchiveUnit());
                     }
                     if(!StringUtils.isBlank(cmsJudVolume.getVolTotalPage())){
                         appraisalCommonResult.setApprPageNum(cmsJudVolume.getVolTotalPage());
                     }
                     if(!StringUtils.isBlank(cmsJudVolume.getVolCaseNum())){
                         appraisalCommonResult.setApprNo(cmsJudVolume.getVolCaseNum());
                     }
                     if(!StringUtils.isBlank(cmsJudVolume.getVolTitle())){
                         appraisalCommonResult.setApprTitle(cmsJudVolume.getVolTitle());
                     }
                     if(!StringUtils.isBlank(cmsJudVolume.getVolItemNumber())){
                         appraisalCommonResult.setApprItemNum(cmsJudVolume.getVolItemNumber());
                     }
                     if(!StringUtils.isBlank(cmsJudVolume.getVolSecret())){
                         appraisalCommonResult.setApprSecret(cmsJudVolume.getVolSecret());
                     }
                     if(!"".equals(cmsJudVolume.getVolArchiveDate())){
                         appraisalCommonResult.setApprArcDate(cmsJudVolume.getVolArchiveDate());
                     }
                     appraisalCommonResult.setApprStatus(cmsAppraisal.getApprStatus());
                     apps.add(appraisalCommonResult);
                 }else if (apprType == 1) {
                     AppraisalCommonResult appraisalCommonResult = new AppraisalCommonResult();
                     //鉴定表编号
                     appraisalCommonResult.setApprCode(cmsAppraisal.getApprCode());
                     //鉴定日期
                     appraisalCommonResult.setApprDate(cmsAppraisal.getApprDate());
                     //意见
                     appraisalCommonResult.setApprOpinion(cmsAppraisal.getAppExtrafour());
                     //结论
                     appraisalCommonResult.setApprResult(cmsAppraisal.getAppExtratwo());
                     //处理单号
                     appraisalCommonResult.setApprDealNum(cmsAppraisal.getAppDealcode());
                     appraisalCommonResult.setApprId(cmsAppraisal.getApprId());
                     List<Map<String,Object>>  arichives = arcAppraisalService.findArchiveTypeByArchiveNum(apprArcId.toString());
                     for (Map<String,Object> map:arichives) {
                         //案卷分类
                         if(map.containsKey("VOL_CLASS")){
                             appraisalCommonResult.setApprVolClass(map.get("VOL_CLASS").toString());
                         }
                         //页数
                         if(map.containsKey("ITEM_PAGE")){
                             appraisalCommonResult.setApprPageNum(map.get("ITEM_PAGE").toString());
                         }
                         //文号
                         if(map.containsKey("ITEM_NAME")){
                             appraisalCommonResult.setApprItemName(map.get("ITEM_NAME").toString());
                         }
                         //归档单位
                         if(map.containsKey("VOL_ARCHIVE_UNIT")){
                             appraisalCommonResult.setApprArcOrg(map.get("VOL_ARCHIVE_UNIT").toString());
                         }
                         //密级
                         if(map.containsKey("VOL_SECRET")){
                             appraisalCommonResult.setApprSecret(map.get("VOL_SECRET").toString());
                         }
                         //保存期限
                         if(map.containsKey("VOL_STORAGE_TIME")){
                             appraisalCommonResult.setApprStorageTime(map.get("VOL_STORAGE_TIME").toString());
                         }
                         //题名
                         if(map.containsKey("ITEM_TITLE")){
                             appraisalCommonResult.setApprTitle(map.get("ITEM_TITLE").toString());
                         }
                         //归档日期
                         if(map.containsKey("ITEM_STR11_DATE")){
                             appraisalCommonResult.setApprArcDate((Date)map.get("ITEM_STR11_DATE"));
                         }
                         //档号
                         if(map.containsKey("ITEM_STR1")){
                             appraisalCommonResult.setApprNo(map.get("ITEM_STR1").toString());
                         }
                         //件数
                         appraisalCommonResult.setApprItemNum("1");
                         appraisalCommonResult.setApprStatus(cmsAppraisal.getApprStatus());
                         apps.add(appraisalCommonResult);
                     }
                 }else{
                     AppraisalCommonResult appraisalCommonResult = new AppraisalCommonResult();
                     //鉴定表编号
                     appraisalCommonResult.setApprCode(cmsAppraisal.getApprCode());
                     //鉴定日期
                     appraisalCommonResult.setApprDate(cmsAppraisal.getApprDate());
                     //意见
                     appraisalCommonResult.setApprOpinion(cmsAppraisal.getAppExtrafour());
                     //结论
                     appraisalCommonResult.setApprResult(cmsAppraisal.getAppExtratwo());
                     //处理单号
                     appraisalCommonResult.setApprDealNum(cmsAppraisal.getAppDealcode());
                     appraisalCommonResult.setApprId(cmsAppraisal.getApprId());
                     appraisalCommonResult.setApprItemNum("1");
                     List<CmsSinItem> cmsSinItems = arcAppraisalService.selectSinItemByItemId(Long.valueOf(apprArcId));
                     CmsSinItem cmsSinItem = cmsSinItems.get(0);
                     if(!StringUtils.isBlank(cmsSinItem.getItemExp1())){
                         appraisalCommonResult.setApprVolClass(cmsSinItem.getItemExp1());
                     }
                     if(!StringUtils.isBlank(cmsSinItem.getItemExp2())){
                         appraisalCommonResult.setApprArcOrg(cmsSinItem.getItemExp2());
                     }
                     if(!StringUtils.isBlank(cmsSinItem.getItemPage().toString())){
                         appraisalCommonResult.setApprPageNum(cmsSinItem.getItemPage().toString());
                     }
                     if(!StringUtils.isBlank(cmsSinItem.getItemStr1())){
                         appraisalCommonResult.setApprNo(cmsSinItem.getItemStr1());
                     }
                     if(!StringUtils.isBlank(cmsSinItem.getItemTitle())){
                         appraisalCommonResult.setApprTitle(cmsSinItem.getItemTitle());
                     }
                     if(!StringUtils.isBlank(cmsSinItem.getItemNum())){
                             appraisalCommonResult.setApprStorageTime(cmsSinItem.getItemNum());
                     }
                     if(!StringUtils.isBlank(cmsSinItem.getItemSecret())){
                         appraisalCommonResult.setApprSecret(cmsSinItem.getItemSecret());
                     }
                     if(!"".equals(cmsSinItem.getItemStr1Date())){
                         appraisalCommonResult.setApprArcDate(cmsSinItem.getItemStr1Date());
                     }
                     appraisalCommonResult.setApprStatus(cmsAppraisal.getApprStatus());
                     apps.add(appraisalCommonResult);
                 }
        }
            List<AppraisalCommonResult> finalList = new ArrayList<AppraisalCommonResult>();
            PageUtils<AppraisalCommonResult> pageUtils = new PageUtils<AppraisalCommonResult>();
            finalList = pageUtils.pageList(apps,offset,limit);
            result.put("rows",finalList);
            result.put("total",apps.size());
        return result;
    }
    /**
     * 根据鉴定表Id查询鉴定信息
     * @param response
     * @param request
     * @return
     */
    @ApiOperation(value = "根据鉴定表Id查询鉴定信息")
    @RequestMapping(value = "/loadApprMessageByApprId")
    @ResponseBody
    public Object loadApprMessageByApprId(HttpServletResponse response, HttpServletRequest request) {
        String apprId = request.getParameter("apprId");
        CmsAppraisal cmsAppraisal = arcAppraisalService.loadApprMessageByApprId(apprId);
        return cmsAppraisal;
    }
    /**
     * 添加鉴定信息
     *
     * @return user
     */
    @ApiOperation(value = "更新鉴定结果信息")
    @RequestMapping(value = "/updateAppraisalMessage")
    @ResponseBody
    public TaotaoResult updateAppraisalMessage(HttpServletRequest request, HttpServletResponse response) {
        TaotaoResult result = new TaotaoResult();
        String apprId = request.getParameter("apprId");
        String apprCode = request.getParameter("apprCode");
        String apprApartment = request.getParameter("apprApartment");
        String apprPerson = request.getParameter("apprPerson");
        String apprDate = request.getParameter("apprDate");
        String apprOldDate = request.getParameter("apprOldDate");
        String apprRemarks = request.getParameter("apprRemarks");
        String apprArcview = request.getParameter("apprArcview");
        String apprPersonview = request.getParameter("apprPersonview");
        String apprExportsview = request.getParameter("apprExportsview");
        String apprLeaderview = request.getParameter("apprLeaderview");
        String apprScope = request.getParameter("apprScope");
        String apprResult = request.getParameter("apprResult");
        String nextDeadline = request.getParameter("nextDeadline");
            //更新
            result = arcAppraisalService.updateAppraisalResultMessage(apprId, apprCode, apprApartment, apprPerson, apprDate,
                    apprArcview,apprPersonview, apprExportsview, apprLeaderview,apprScope,apprResult,nextDeadline,apprOldDate,apprRemarks);
        return result;
    }


    @ApiOperation(value = "查询保密期限的到期的档案")
    @RequestMapping(value = "/showAllAppraisalList")
    @ResponseBody
    public Object  showAllAppraisalList(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        Map<String, Object> result = new HashMap<>(2);
        List<AppraisalItemResult> itemList = arcAppraisalService.selectAllNeedAppraisalArc();
        List<AppraisalItemResult> finalList = new ArrayList<AppraisalItemResult>();
        PageUtils<AppraisalItemResult> pageUtils = new PageUtils<AppraisalItemResult>();
        finalList = pageUtils.pageList(itemList,offset,limit);
        result.put("rows",finalList);
        result.put("total",itemList.size());
        return result;
    }
}